package com.diyunapp.xilinapp.selfdefine;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by NIDAYE on 2016/8/1.
 */
public class SearchDBHelper extends SQLiteOpenHelper {

    public SearchDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public SearchDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("zhangxiaoxidebug","SearchDBHelper onCreate create needs tables-------");
        db.execSQL("CREATE TABLE search_list_table(_id integer primary key autoincrement,search_info varchar(200) UNIQUE)");
        db.execSQL("CREATE TABLE search_content_table(_id integer primary key autoincrement,content_id varchar(30),content_info varchar(200) UNIQUE)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("zhangxiaoxidebug","SearchDBHelper onUpgrade drop the tables-------");
        String sql ="DROP TABLE search_list_table";
        db.execSQL(sql);
        String sql1 ="DROP TABLE search_content_table";
        db.execSQL(sql1);
    }

    /**
     * 判断某张表是否存在
     * @param tabName 表名
     * @return
     */
    public boolean tabIsExist(String tabName){
        SQLiteDatabase mydatabase = this.getReadableDatabase();
        boolean result = false;
        if(tabName == null){
            return false;
        }

        try {
            String sql = "select count(*) as c from sqlite_master where type = 'table' and name ='"+tabName.trim()+"'" ;
            Cursor cursor = mydatabase.rawQuery(sql, null);
            if(cursor.moveToNext()){
                int count = cursor.getInt(0);
                if(count>0){
                    result = true;
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return result;
    }

    public void insertList(String searchInfo)
    {
        SQLiteDatabase mydatabase = this.getReadableDatabase();
        ContentValues values =new ContentValues();
        values.put("search_info", searchInfo);
        mydatabase.insert("search_list_table",null,values);
    }

    public void clearList()  //清空表
    {
        SQLiteDatabase mydatabase = this.getReadableDatabase();
        //删除表的SQL语句
        String sql ="DROP TABLE search_list_table";
        //先删除
        mydatabase.execSQL(sql);
        //再创建
        mydatabase.execSQL("CREATE TABLE search_list_table(_id integer primary key autoincrement,search_info varchar(200) UNIQUE)");
    }


    public void insertContent(String contendId,String contendInfo)
    {
        SQLiteDatabase mydatabase = this.getReadableDatabase();
        ContentValues values =new ContentValues();
        values.put("content_id", contendId);
        values.put("content_info", contendInfo);
        mydatabase.insert("search_content_table",null,values);
    }

    public void clearContent()  //清空表
    {
        SQLiteDatabase mydatabase = this.getReadableDatabase();
        if(tabIsExist("search_content_table"))
        {
            //删除表的SQL语句
            String sql ="DROP TABLE search_content_table";
            //先删除
            mydatabase.execSQL(sql);
        }
        //再创建
        mydatabase.execSQL("CREATE TABLE search_content_table(_id integer primary key autoincrement,content_id varchar(30),content_info varchar(200) UNIQUE)");
    }

    public int contentNum()  //查询表的总数
    {
        SQLiteDatabase mydatabase = this.getReadableDatabase();
        if(!tabIsExist("search_content_table"))
        {
            return 0;
        }
        //查询表的总数
        Cursor cursor = mydatabase.rawQuery("select count(*) from search_content_table",null);
        //游标移到第一条记录准备获取数据
        cursor.moveToFirst();
        // 获取数据中的LONG类型数据
        int count = cursor.getInt(0);
        return  count;
    }
}
